<?php

/**
 * This is the model class for table "{{member_transfer}}".
 *
 * The followings are the available columns in table '{{member_transfer}}':
 * @property string $id
 * @property string $uid
 * @property string $username
 * @property string $phone
 * @property string $alipay
 * @property string $alipay_name
 * @property string $add_time
 * @property integer $status
 * @property string $cause
 */
class MembersTransfer extends BaseModel {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{members_transfer}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('uid, username', 'required'),
            array('phone', 'match', 'pattern' => '/^1[3458][0-9]{9}$/', 'message' => '手机号必须为1开头的11位纯数字'),
            array('phone,alipay_name,alipay,taobao_user_nick', 'checkUnique'),
            array('status,cause,add_time,update_time', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('uid, username, phone, alipay, alipay_name, add_time, status, cause', 'safe', 'on' => 'search'),
        );
    }

    public function checkUnique() {
        //手机号需要唯一

        $objInfoModel = MembersModel::model()->findByPk($this->uid);
        if ($objInfoModel->phone) {
            $this->phone = '';
        } else {
            if (!$this->phone) {
                $this->addError('phone', '手机号码不能为空');
            }
            if (MembersModel::getByPhone($this->phone)) {
                $this->addError('phone', '该手机已经被注册！');
            }
            if ($this->getByPhone($this->phone)) {
                $this->addError('phone', '该手机号已经有人提交还未审核！');
            }
        }
        if ($objInfoModel->alipay) {
            $this->alipay = '';
        } else {
            if (!$this->alipay) {
                $this->addError('alipay', '支付宝账号不能为空');
            }
        }
        if ($objInfoModel->alipay_name) {
            $this->alipay_name = '';
        } else {
            if (!$this->alipay_name) {
                $this->addError('alipay_name', '支付宝姓名不能为空');
            }
        }

        $objTaobaoModel = MembersTaobaoModel::model()->getMemberTB($this->uid);
        if ($objTaobaoModel && $objTaobaoModel->taobao_user_nick) {
            $this->taobao_user_nick = '';
        } else {
            if (!$this->taobao_user_nick) {
                $this->addError('taobao_user_nick', '旺旺号不能为空');
            }
        }
    }

    public function getByPhone($phone) {
        if ($phone) {
            if ($this->uid) {
                return self::model()->find(array('condition' => 'phone = :phone AND status = 0 AND uid != :uid', 'params' => array('phone' => $phone, 'uid' => $this->uid)));
            } else {
                return self::model()->find(array('condition' => 'phone = :phone AND status = 0', 'params' => array('phone' => $phone)));
            }
        }
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'uid' => '会员ID',
            'username' => '用户名',
            'phone' => '手机号',
            'alipay' => '支付宝账号',
            'alipay_name' => '支付宝名称',
            'add_time' => '添加时间',
            'status' => '处理状态',
            'cause' => '转换原因',
            'taobao_user_nick' => '旺旺号',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('uid', $this->uid, true);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('alipay', $this->alipay, true);
        $criteria->compare('alipay_name', $this->alipay_name, true);
        $criteria->compare('add_time', $this->add_time, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('cause', $this->cause, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return MembersTransfer the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function beforeSave() {
        $time = time();
        $this->update_time = $time;
        $this->cause = CHtml::encode($this->cause);
        if ($this->isNewRecord) {
            $this->status = 0;
            $this->add_time = $time;
        }
        return true;
    }

    public function editor($data, $userinfo = '') {
        empty($userinfo) ? $userinfo = yii::app()->user->getInfo() : '';

        $this->attributes = $data;
        $this->uid = $userinfo['uid'];
        $this->username = $userinfo['username'];
        return $this->save();
    }

    public static function getStatusHtmlDetail($intStatus) {
        $arrData = self::getStatusHtmlList();
        return $arrData[$intStatus];
    }

    public static function getStatusHtmlList() {
        return array(
            0 => '未处理',
            1 => '已处理',
            2 => '已拒绝',
        );
    }

    public function createSearchCriteria() {

        $criteria = new CDbCriteria;

        $criteria->compare('uid', $this->uid);
        $criteria->compare('username', $this->username);
        $criteria->compare('status', $this->status);


        return $criteria;
    }

    public function updateScoreExperience($objMember = '') {
        if (!is_object($objMember)) {
            $objMember = MembersModel::model()->findByPk($objMember);
        }
        if (!$objMember || $objMember->kind == 0) {
            return false;
        }
        $objMember->experience = 0;
        $objMember->kind = 0;
        $objMember->score = 0;
        empty($objMember->alipay) ? $objMember->alipay = $this->alipay : '';
        empty($objMember->alipay_name) ? $objMember->alipay_name = $this->alipay_name : '';
        empty($objMember->phone) ? $objMember->phone = $this->phone : '';
        $objMember->save(false);

        if ($this->taobao_user_nick) {
            MembersTaobaoModel::model()->saveWangwang($objMember->uid, $this->taobao_user_nick);
        }

        $this->status = 1;
        $this->save(false, array('status'));

        //生成记录
        MembersActionModel::model()->addAction($objMember->uid, MembersActionModel::action_system, '买家转为商家,积分清零');
        return true;
    }

}
